package it.polimi.swim.servlet;

import it.polimi.swim.entity.HelpRequest;
import it.polimi.swim.entity.SystemUser;
import it.polimi.swim.session.HelpRequestManagerBean;
import it.polimi.swim.session.HelpRequestManagerBeanRemote;
import it.polimi.swim.session.SkillManagerBean;
import it.polimi.swim.session.SkillManagerBeanRemote;
import it.polimi.swim.session.SystemUserManagerBean;
import it.polimi.swim.session.SystemUserManagerBeanRemote;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class MyHelpServlet
 */
public class MyHelpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public MyHelpServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			try {
				System.out.println("post");
				
				Properties env = new Properties();
				env.put(Context.INITIAL_CONTEXT_FACTORY,
						"org.jnp.interfaces.NamingContextFactory");
				env.put(Context.PROVIDER_URL, "localhost:1099");
				InitialContext jndiContext = new InitialContext(env);

				System.out.println("Chiamo il sesison");
				InitialContext ctx = new InitialContext();
				Object ref = ctx.lookup(SystemUserManagerBean.REMOTE);
				SystemUserManagerBeanRemote systemUserManager = (SystemUserManagerBeanRemote) ref;

				HttpSession session = request.getSession();
				int idUser = (Integer) session.getAttribute("id");
				SystemUser user = systemUserManager.searchUserById(idUser);
				List<HelpRequest> createdHelps = systemUserManager.getUserCompletedHelp(idUser);
				request.setAttribute("createdHelps", createdHelps);
				
			request.getRequestDispatcher("myHelp.jsp").forward(request, response);

				
			} catch (Exception e) {
				e.printStackTrace();
			}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
